package com.scientificInfo.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scientificInfo.bean.*;
import com.scientificInfo.entity.TSMResult;
import com.scientificInfo.server.*;
import com.scientificInfo.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TeacherController {

    @Autowired
    TeacherServer teacherServer;


    @Autowired
    AdminServer adminServer;

    //查询教师发布的公告
    @RequestMapping("/sel_sys_msg")
    @ResponseBody
    public TSMResult selectSystemMsg() {
        TSMResult tsmResult = new TSMResult();
        tsmResult.setMessage("查询系统信息成功");
        tsmResult.setCode(200);
        tsmResult.setData(teacherServer.selectSystemMsg());
        return tsmResult;
    }

    //查询管理员发给教师的消息
    @RequestMapping("/sel_personal_msg")
    @ResponseBody
    public TSMResult selectPersonalMsg(@RequestParam("teacherId") String teacherId) {
        TSMResult tsmResult = new TSMResult();
        tsmResult.setData(teacherServer.selectMsgByTeacherId(teacherId));
        tsmResult.setCode(200);
        tsmResult.setMessage("查询私信成功");
        return tsmResult;
    }

    //暂时没用
    @RequestMapping(value = "/getTeacherName")
    @ResponseBody
    public TSMResult getTeacherName(HttpSession session) {
        TSMResult tsmResult = new TSMResult();
        String id = (String) session.getAttribute("id_infor");
        List<Teacher> teachers = teacherServer.selectTeacherById(id);
        if (teachers.size() > 0) {
            tsmResult.setData(teachers.get(0).getTeachername());
            tsmResult.setMessage("查询成功！");
        } else {
            tsmResult.setMessage("用户名不存在！");
        }
        return tsmResult;
    }

    //暂时没用
    @GetMapping("/getTeacherNameById")
    @ResponseBody
    public TSMResult getTeacherNameById(@RequestParam("meetingid") int meetingid) {
        List<Teacher> teachers = teacherServer.getNameByMeetingId(meetingid);
        for (Teacher teacher : teachers) {
            System.out.println(teacher.getTeachername());
        }
        return new TSMResult(200, "成功", teachers);
    }

    //更改教师信息
    @RequestMapping(value = "/updateTeacherInfor")
    @ResponseBody
    public TSMResult updateTeacherInfor(@RequestBody(required = false) Teacher teacher) {
        TSMResult tsmResult = new TSMResult();
        System.out.println(teacher);
        if (teacher.getTeacherid() == null) {
            teacher.setTeacherid(UuidUtil.getUuid(6));
            teacher.setTeacherstatu(1);
            int data = adminServer.insertTeacher(teacher);
            tsmResult.setMessage((data == 0) ? "教师创建失败！" : "教师创建成功！");
            tsmResult.setData(data);
            return tsmResult;
        }
        teacherServer.updateTeacher(teacher);
        tsmResult.setMessage("教师信息更改成功！");
        return tsmResult;
    }

    //通过id获得教师信息
    @GetMapping(value = "/getTeacherInfor")
    @ResponseBody
    public TSMResult getTeacherInfor(TSMResult tsmResult, @RequestParam("teacherId") String teacherId) {
        List<Teacher> teachers = teacherServer.selectTeacherById(teacherId);
        tsmResult.setMessage("教师信息查询成功！");
        tsmResult.setData(teachers.get(0));
        return tsmResult;
    }

    //添加著作 暂时没用
    @RequestMapping("/add_works")
    @ResponseBody
    public TSMResult insertWorks(Works works) {
        works.setResultuuid(UuidUtil.getUuid(6));
        System.out.println(teacherServer.insertWorks(works));
        return new TSMResult(200, "成功", works);
    }

    //暂时没用
    @RequestMapping("/add_meeting")
    @ResponseBody
    public TSMResult takePartInMeeting(Meeting meeting, HttpSession session) {
        String message = "会议参加成功，请前往会议中心查看";
        int flag = teacherServer.insertMeeting(meeting);

        Meeting meeting1 = teacherServer.selectOneMeeting(meeting.getMeetingname());
        System.out.println("该会议对应的数据为：" + meeting1);

        //根据将对应教师的meetingid设置成这个meetingid
        int newMeetingId = meeting1.getMeetingid();

        String teacher_id = (String) session.getAttribute("id_infor");

        teacherServer.updateTeacherMeetingById(teacher_id, newMeetingId);
        if (flag == 0) {
            message = "抱歉，会议参加失败，请重新操作，谢谢。";
        }
        return new TSMResult(200, message, flag);
    }

    //新增论文
    @RequestMapping("/add_paper")
    @ResponseBody
    public TSMResult addPaper(@RequestBody(required = false) Paper paper, Model model) {
        TSMResult tsmResult = new TSMResult();
        paper.setResultuuid(UuidUtil.getUuid(6));
        paper.setCheckflag(0);
        paper.setTeacherid(paper.getTeacherid());
        int code = teacherServer.insertPaper(paper);
        tsmResult.setData(code);
        tsmResult.setMessage("论文添加成功！");
        return tsmResult;
    }

    //新增专利
    @RequestMapping("/add_patent")
    @ResponseBody
    public TSMResult addPatent(@RequestBody(required = false) Patent patent, HttpSession session) {
        TSMResult tsmResult = new TSMResult();
        patent.setResultuuid(UuidUtil.getUuid(6));
        patent.setCheckflag(0);
        int code = teacherServer.insertPatent(patent);
        tsmResult.setData(code);
        tsmResult.setMessage("专利添加成功！");
        return tsmResult;
    }

    //新增项目
    @RequestMapping("/add_project")
    @ResponseBody
    public TSMResult addProject(Project project, HttpSession session) {
        TSMResult tsmResult = new TSMResult();
        project.setResultuuid(UuidUtil.getUuid(6));
        project.setCheckflag(0);
        project.setTeacherid((String) session.getAttribute("id_infor"));
        teacherServer.insertProject(project);
        tsmResult.setMessage("项目添加成功！");
        tsmResult.setCode(200);
        tsmResult.setData(new Integer(1));
        return tsmResult;
    }


    //根据id查询教师的专利
    @RequestMapping("/show_patent")
    @ResponseBody
    public TSMResult showPatent(String Id, int ope, @RequestParam(value = "pn", defaultValue = "1") int pn) {
        TSMResult tsmResult = new TSMResult();
        PageHelper.startPage(pn, 5);
        List<Patent> list = teacherServer.selectPatent(ope, Id);
        PageInfo page = new PageInfo(list, 5);
        tsmResult.setData(page);
        tsmResult.setMessage("showPatent");
        tsmResult.setCode(200);
        return tsmResult;
    }

    //根据id查询教师的论文
    @RequestMapping("/show_paper")
    @ResponseBody
    public TSMResult showPaper(
            String Id,
            int ope,
            @RequestParam(value = "pn", defaultValue = "1") int pn,
            @RequestParam(value = "pageSize") int pageSize) {
        //ope 代表论文的状态
        // 如果是1代表已经审核通过 如果是0代表已提交等待审核通过
        //如果是3代表已被下架

        TSMResult tsmResult = new TSMResult();
        PageHelper.startPage(pn, pageSize);
        List<Paper> list = teacherServer.selectPaperByTeacherId(ope, Id);
        PageInfo page = new PageInfo(list, 5);
        tsmResult.setData(page);
        tsmResult.setMessage("showPaper");
        tsmResult.setCode(200);
        return tsmResult;
    }

    //根据id查询教师的项目
    @RequestMapping("/show_project")
    @ResponseBody
    public TSMResult showProject(String Id, int ope, @RequestParam(value = "pn", defaultValue = "1") int pn) {
        TSMResult tsmResult = new TSMResult();
        PageHelper.startPage(pn, 5);
        List<Project> list = teacherServer.selectProject(ope, Id);
        PageInfo page = new PageInfo(list, 5);
        tsmResult.setData(page);
        tsmResult.setMessage("showPatent");
        tsmResult.setCode(200);
        return tsmResult;
    }

    //暂时没用
    @RequestMapping("/updateMeeting")
    @ResponseBody
    public TSMResult takeInMeeting(HttpSession session, @RequestParam("meetingId") int meetingId) {
        teacherServer.updateTeacherMeetingById((String) session.getAttribute("id_infor"), meetingId);

        return new TSMResult(200, "会议加入成功", meetingId);
    }

    //参加会议
    @RequestMapping("/takeMeeting")
    @ResponseBody
    public TSMResult takeMeeting(@RequestParam("meetingid") int meetingid, @RequestParam("teacherId") String teacherId) {
        String message = "参加会议成功！";
        //如果返回值为0 则参加失败 如果是2 则已经参加过了
        int data = teacherServer.takeMeeting(meetingid, teacherId);
        if (data == 0) {
            message = "抱歉，参与失败！";
        } else if (data == 2) {
            message = "您已参加!";
        }
        return new TSMResult(200, message, data);
    }

}
